package com.netty.cn.rpc.jboss.client;

import com.netty.cn.rpc.jboss.model.SubscribeReq;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;

/**
 * 
 * @author wangzonghui
 * @date 2021-07-20 10:09:00
 * @Description 客户端chandler
 */
public class SubReqClientHandler extends ChannelInboundHandlerAdapter {


    public SubReqClientHandler() {
    }

    @Override
    public void channelActive(ChannelHandlerContext ctx) {
        for (int i = 0; i < 10; i++) {
            ctx.write(subReq(i));
        }
        ctx.flush();
    }

    /**
     * 客户端消息封装方法
     * @param i
     * @return
     */
    private SubscribeReq subReq(int i) {
        SubscribeReq req = new SubscribeReq();
        req.setAddress("Beijing Jiaotong University, BeiJing ");
        req.setPhoneNumber("010-5168****");
        req.setProductName("Test For Marshalling");
        req.setSubReqID(i);
        req.setUserName("LMRZero");
        return req;
    }

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg)
            throws Exception {
        System.out.println("服务端返回消息 : [" + msg + "]");
    }

    @Override
    public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
        ctx.flush();
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
        cause.printStackTrace();
        ctx.close();
    }
}
